How To Retrieve Data In Laravel

admin_img Posted By Bajarangi soft , Posted On 20-01-2021

We can retrieve data in table using Laravel with modal.

How-To-Retrieve-Data-Using-Ajax-In Laravel

Step: 1 Create a new table like product and insert some rows of data and make a model.
In this step, we will create first post table and model.
 

public $table = 'product';

    protected $fillable = [
        'product_name', 'product_title',
    ];


Step: 2 Make a view page and create ajax for fetching data.
resources/views/welcome.blade.php

 

<html>
  <head>
   <title>Laravel CRUD Application using Ajax without Reloading Page</title>  
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"> 
  </head>
<body><br>
<div id="alert" class="alert-success new-window" data-message="my message">
        </div>
<div class="container">
<div class="panel panel-primary">
 <div class="panel-heading">Products
 <button id="btn_add" name="btn_add" class="btn btn-default pull-right">Add New Product</button>
    </div>
      <div class="panel-body"> 
       <table class="table">
        <thead>
          <tr>
            <th>ID</th>
            <th>Product Name</th>
            <th>Product Title</th>
            <th>Actions</th>
          </tr>   
         </thead>
         <tbody id="products-list" name="products-list">
           @foreach ($product as $product)
            <tr id="product">
             <td>{{$product->id}}</td>
             <td>{{$product->product_name}}</td>
             <td>{{$product->product_title}}</td>
              <td>
              <button class="btn btn-warning btn-detail open_modal btn-edit" value="{{$product->id}}">Edit</button>
              <button class="btn btn-danger btn-delete delete-product" value="{{$product->id}}">Delete</button>
              </td>
            </tr>
             @endforeach
        </tbody>
        </table>
       </div>
       </div>
</body>
</html>


Step: 3 Create a function in controller for get data from the model.
app/Http/Controllers/ProductController.php

 

public function index()
    {        
        $product = Contry::get();
        return view('blog',compact('product'));   
    }


Step: 4 Make a route for connection with controller.
app/Http/routes.php
 

Route::get('blog', 'ProductController@index');

Related Post